package org.shagin.jfin.sheet;

import java.text.DecimalFormat;

import org.shagin.jfin.core.Utils;

public class SheetRowTotalPercent extends SheetRow {
	private static DecimalFormat fmt = new DecimalFormat("#0.00");

	private SheetRowSubTotal totalRow;

	public SheetRowTotalPercent(SheetRowSubTotal totalRow, SheetColumnLabel col) {
		super();
		this.totalRow = totalRow;
		setString(col, totalRow.getString(col, "") + ", %");
	}

	public void update(Sheet sheet) {
		double totalExpense = totalRow.getDouble(sheet.totalSaldo.colExpense);

		for (SheetColumn<?> col : sheet.getData().getColumns()) {

			if (Utils.isEqualsAny(col, sheet.totalSaldo.colSaldo))
				continue;

			if (col instanceof SheetColumnDouble) {
				Double value = totalRow.getDouble(col);
				if (Utils.isZero(totalExpense))
					value = 0d;
				else
					value = value / totalExpense;
				setString(col, fmt.format(value * 100) + " %");
			}
		}
	}
}
